package cn.com.gzqd.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.gzqd.bean.Page;
import cn.com.gzqd.daoimpl.BookDaoImpl;
import cn.com.gzqd.model.Book;
import cn.com.gzqd.util.MySQLHelp;
import cn.com.gzqd.serviceimpl.BookServiceImpl;

public class PageServlet extends HttpServlet {
	
	 /**
	 * 
	 */
	BookDaoImpl dao=new BookDaoImpl();
	private static final long serialVersionUID = 1L;
		//doPost鏂规硶
		public void doPost(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			    request.setCharacterEncoding("UTF-8");
	   		    response.setCharacterEncoding("UTF-8");
			try {  
				
	                    difpage(request,response);
				      
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (SQLException e) {
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
		}
		//doGet鏂规硶
		public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	       doPost(request,response);
	    }
		
	

public Page setpage(HttpServletRequest request, HttpServletResponse response) throws Exception{
	String crd=request.getParameter("currentRecord");
	String keyWord="";
	try{		
		keyWord=request.getParameter("xs");
		if(keyWord==null){
			keyWord="";
		}	}catch(Exception e){
			e.printStackTrace();
		}	
	System.out.println("setpage:"+keyWord);
	ArrayList<Book> result=dao.findAll(keyWord);
	Page pager=new Page();
	pager.setTotalRecord(result.size());//获取记录长度 
	pager.setTotalPage(result.size(),pager.getPageSize());//获取指定页长度的页数
	if(crd!=null)
    {
		int currentRecord=Integer.parseInt(crd);
        pager.setCurrentRecord(currentRecord);
        pager.setCurrentPage(currentRecord,pager.getPageSize());
    }
	return pager;
	
}

public void difpage(HttpServletRequest request, HttpServletResponse response) throws Exception{	
	String keyWord="";
	try{		
		keyWord=request.getParameter("xs");
		if(keyWord==null){
			keyWord="";
		}		
	System.out.println(keyWord);
	ArrayList<Book> result=dao.findAll(keyWord); //获取全部列表	
	 System.out.println("获取全部列表");
	 Page pager=new Page();
	 pager=setpage(request,response);
	     List<Book> subResult=null;
	     int currentRecord=pager.getCurrentRecord();
     if(currentRecord==0){
     	if(pager.getTotalRecord()<8){
     		subResult=(List<Book>) result.subList(0,pager.getTotalRecord());
     	}
     	else{
     		subResult=(List<Book>) result.subList(0,pager.getPageSize());
     	}         
     }
     else if(pager.getCurrentRecord()+pager.getPageSize()<result.size())
     {
           subResult=(List<Book>) result.subList(pager.getCurrentRecord(),pager.getCurrentRecord()+pager.getPageSize());
     }
     else
     {
          subResult=(List<Book>) result.subList(pager.getCurrentRecord(),result.size());
     }
     request.setAttribute("pager", pager);
     request.setAttribute("subResult", subResult);
	 request.getRequestDispatcher("list.jsp").forward(request, response);
	}catch(Exception e){
		e.printStackTrace();
	}
 }


}